package io.jee.runshan.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileTool {


	// 复制文件
    public static void copyFile(File sourceFile, File targetFile) throws IOException {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        if(!targetFile.getParentFile().exists()){
        	targetFile.getParentFile().mkdirs();
        }
        if(!targetFile.exists()){
        	targetFile.createNewFile();
        }
        try {
            // 新建文件输入流并对它进行缓冲
            inBuff = new BufferedInputStream(new FileInputStream(sourceFile));

            // 新建文件输出流并对它进行缓冲
            outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));

            // 缓冲数组
            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            // 刷新此缓冲的输出流
            outBuff.flush();
        } finally {
            // 关闭流
            if (inBuff != null)
                inBuff.close();
            if (outBuff != null)
                outBuff.close();
        }
    }

	/**
	 * 删除某个文件夹下的所有文件夹和文件
	 * 
	 * @param delpath
	 *            String
	 * @throws FileNotFoundException
	 * @throws IOException
	 * @return boolean
	 */
	public static boolean deletefile(String delpath) throws Exception {
		try {
			File file = new File(delpath);
			// 当且仅当此抽象路径名表示的文件存在且 是一个目录时，返回 true
			if (!file.isDirectory()) {
				file.delete();
			} else if (file.isDirectory()) {
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++) {
					File delfile = new File(delpath + "\\" + filelist[i]);
					if (!delfile.isDirectory()) {
						delfile.delete();
						System.out
								.println(delfile.getAbsolutePath() + "删除文件成功");
					} else if (delfile.isDirectory()) {
						deletefile(delpath + "\\" + filelist[i]);
					}
				}
				System.out.println(file.getAbsolutePath() + "删除成功");
				file.delete();
			}

		} catch (FileNotFoundException e) {
			System.out.println("deletefile() Exception:" + e.getMessage());
		}
		return true;
	}

	public static void main(String[] args) throws IOException {
		// List resFileList = new ArrayList();
		// resFileList.add(new File("C:\\new.gif"));
		// resFileList.add(new File("C:\\HelloWorld.java"));
		// resFileList.add(new File("C:\\crebas.sql"));
		// resFileList.add(new File("E:\\log.log"));
		// resFileList.add(new File("C:\\ooo\\upx\\"));
		// ZipTool.zipFiles(resFileList, zipFile);

	}

	public static File getImage(File inputFile, String NO) {
		for (File file : inputFile.listFiles()) {
			if (file.isDirectory()) {
				String name = file.getName();
				if (name.equals(NO)) {
					return file;
				} else {
					File file1 = getImage(file, NO);
					if (file1 == null) {
						continue;
					} else {
						return file1;
					}
				}
			} else {
				continue;
			}
		}
		return null;
	}

	public static File getTopImage(File inputFile, String NO) {
		return new File(getImage(inputFile, NO), "top");
	}

	public static File getExcel(File inputFile) {
		for (File file : inputFile.listFiles()) {
			if (file.isDirectory()) {
				File file1 = getExcel(file);
				if (file1 == null) {
					continue;
				} else {
					return file1;
				}
			} else {
				String name = file.getName();
				if (name.endsWith(".xls") || name.endsWith(".xlsx")) {
					return file;
				} else {
					continue;
				}
			}
		}
		return null;
	}

}